package bairui.international.weixin.portal.miniapp.controller.order.product;

import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.QTB_ProductOrder;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_ProductOrder;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_User;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_Order_Product_Page_Get 测试
 *
 */
@Controller
public class C_Order_Product_Page_Get
 extends SC_Order_Product_Page_Get 
{

	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_User service_User;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam(required=false, defaultValue="0") Integer state,
		@RequestParam(required=false) String type,
		@RequestParam String skey
	) throws Exception {
		/*
		 * 处理状态
		 */
		BooleanBuilder query = new BooleanBuilder();
		State_ProductOrder orderState = null;
		if(state == 0){
			
		}else if(state == 1){
			orderState = State_ProductOrder.Paying;
		}else if(state == 2){
			orderState = State_ProductOrder.Verifing;
		}else if(state == 3){
			orderState = State_ProductOrder.Sending;
		}else if(state == 4){
			orderState = State_ProductOrder.Sended;
		}else if(state == 5){
			orderState = State_ProductOrder.Finished;
		}
		if(null != orderState){
			query.and(QTB_ProductOrder.tB_ProductOrder.state.eq(orderState));
		}
		/*
		 * 处理类型
		 */
		if(null != type && type.trim().equals("grade")){
			query.and(QTB_ProductOrder.tB_ProductOrder.type.eq(Type_ProductOrder.Grade));
		}else {
			query.and(QTB_ProductOrder.tB_ProductOrder.type.eq(Type_ProductOrder.Buy));
		}
		/*
		 * 
		 */
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		//
		return service_ProductOrder
				.list(
						it->it.user.eq(user).and(query),
						it->new QSort(it.id.desc())
					)
				.getContent()
				.stream()
				.map(order->new Output(
						order.getId(), 
						order.getNumber(),
						order.getPrice(),
						order.getScore(),
						new C_Order_Product_ShowImg_Get.Get.Operate() {
							@Override public Object getId() {
								return order.getId();
							}
						}.getUrl(),
						order.getState().getComment(),
						null== order.getPayWay() ? "" : order.getPayWay().getComment(),
						null==order.getSubmitDate() ? "" : new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(order.getSubmitDate())
					))
				.collect(Collectors.toList());
	}
	
	@AllArgsConstructor
	private class Output {
		private @Getter Long id;
		private @Getter String number;
		private @Getter Double price;
		private @Getter Double score;
		private @Getter String imgUrl;
		private @Getter String state;
		private @Getter String payWay;
		private @Getter String submitDate;
		//
	}

}
